<?php

namespace App\Services;

use Illuminate\Support\HigherOrderTapProxy;

trait ResourceCollection
{
    /**
     * Create a new anonymous resource collection.
     *
     * @param $resource
     * @return AnonymousResourceCollection|HigherOrderTapProxy|mixed
     */
    public static function collection($resource): mixed
    {
        return tap(new AnonymousResourceCollection($resource, static::class), function ($collection) {
            if (property_exists(static::class, 'preserveKeys')) {
                /* @phpstan-ignore-next-line */
                $collection->preserveKeys = (new static([]))->preserveKeys === true;
            }
        });
    }
}
